package com.jiyici.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.jiyici.EditItemActivity;
import com.jiyici.ItemActivity;
import com.jiyici.ItemHolder;
import com.jiyici.R;
import com.jiyici.controller.DataManager;
import com.jiyici.controller.DataManager.DataListener;
import com.jiyici.data.ClientDBHelper;
import com.jiyici.model.Item;
import com.jiyici.view.DialogUtil;
import com.jiyici.view.DialogWrapper.DialogAction;

public class ItemInfoFragment extends BaseFragment implements DataListener {

    private TextView mItemName;
    private TextView mInfo;

    @Override
    public void onItemCatgoryChanged() {
        mItemName.setText(getItem().itemName);
    }
    
    @Override
    public void onRecordChanged() {
        buildItemInfo();
    }
    
    private Item getItem() {
        return ((ItemHolder) getActivity()).getItem();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(R.string.add_one).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add("Table").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add("Calendar").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        SubMenu subMenu = menu.addSubMenu(R.string.menus);
        subMenu.add(R.string.actionbar_item_edit).setIcon(R.drawable.ic_action_edit)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        subMenu.add(R.string.actionbar_item_delete_item).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        subMenu.getItem().setIcon(R.drawable.abc_ic_menu_moreoverflow_normal_holo_dark)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (getText(R.string.actionbar_item_delete_item).equals(item.getTitle())) {
            DialogUtil.showConfirmDialog(getActivity(), getString(R.string.actionbar_item_delete_item),
                    getString(R.string.dialog_confirm_delete_item), new DialogAction() {

                        @Override
                        public int run() {
                            DataManager.deleteItem(getItem());
                            getActivity().finish();
                            return DISMISS;
                        }
                    });
            return true;
        } else if (getText(R.string.actionbar_item_edit).equals(item.getTitle())) {
            Intent intent = new Intent(getActivity(), EditItemActivity.class);
            intent.putExtra("item", getItem());
            getActivity().startActivity(intent);
            return true;
        } else if (getText(R.string.add_one).equals(item.getTitle())) {
            DialogUtil.showAddOneDialog(getActivity(), getItem());
            return true;
        } else if ("Table".equals(item.getTitle())) {
            ((ItemActivity)getActivity()).showTable();
            return true;
        } else if ("Calendar".equals(item.getTitle())) {
            ((ItemActivity)getActivity()).showCalendar();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_item, container, false);
        mItemName = (TextView) view.findViewById(R.id.item_name);
        mItemName.setText(getItem().itemName);
        mInfo = (TextView) view.findViewById(R.id.item_info);
        buildItemInfo();
        return view;
    }

    private void buildItemInfo() {
        new AsyncTask<Void, Void, Void>() {

            private String info = "";

            @Override
            protected Void doInBackground(Void... params) {
                int recordCount = ClientDBHelper.getInstance().dbQueryRecordCount(getItem().getId());
                StringBuffer sb = new StringBuffer();
                sb.append(getString(R.string.record)).append(": ").append(recordCount);
                info = sb.toString();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                mInfo.setText(info);
            }

        }.execute();
    }
}
